package com.jewaton.auth.commons.action;

import com.jewaton.auth.commons.action.exception.AdminLoginTimeoutException;
import com.jewaton.auth.commons.entity.Account;
import com.jewaton.auth.commons.entity.Organization;
import com.jewaton.auth.commons.service.AccountService;
import com.jewaton.auth.commons.util.Constants;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 二次继承，为以后不同的用户可作扩展
 */
public class BaseAdminAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -693347123123621848L;

	private String ctx = null;
	
    @Resource(name="accountService")
    private AccountService accountService;
	
    /**
     * 获取登录Session信息  key = "PC_ACCOUNT"
     * @return Account对象
     */
    protected Account getSessionPCAaccount() {
        Object obj = request.getSession().getAttribute(Constants.SESSION_PC_ACCOUNT);
        if (obj == null) {
            throw new AdminLoginTimeoutException("未登录");
        }
        Account acc = (Account) obj;
        return acc;
    }
    
    /**
     * 获取登录account表主键
     * @return account表主键aid
     */
    protected Long getSessionAid() {
        return getSessionPCAaccount().getAid();
    }

    /**
     * 获取登录所在机构表主键
     * @return ORGANIZATION表主键oid
     */
    protected String getSessionOrgId() {
        return getSessionPCAaccount().getOrganization().getOid();
    }

    /**
     * 获取登录所在区域表主键
     * @return DISTRICT表主键did
     */
    protected String getSessionDistrictId() {
        return getSessionPCAaccount().getOrganization().getDistrict().getDid();
    }
    
    /**
     * 打印excel表头
     * @param htmlTable
     * @param column
     * @param array
     */
    protected void printHeader(StringBuffer htmlTable,String column,String[] array){
    	htmlTable.append("<table align='center' width='100%' border='1' cellspacing='1' cellpadding='1'><thead><tr>");
		if (!"".equals(column)) {
			String[] cid=column.split("#");
			for (int j = 0; j < cid.length; j++) {
				Integer index=Integer.parseInt(cid[j].trim());
				htmlTable.append("<th>"+array[index]+"</th>");
			}
		}else{
			for (int i = 0; i < array.length; i++) {
				htmlTable.append("<th>"+array[i]+"</th>");
			}
		}
		htmlTable.append("</tr></thead><tbody>");    	
    }
    
    /**
     * 根据个人所属机构 显示不同的机构下拉框， 市局组织显示所有<br/>
     * REQUEST_ORG_SELECT_BOX = "orgSelectBox"<br/>
     * 将List&lt;Org&gt; 设置在request中
     */
    protected void setOrgSelectBoxInRequest() {    
        request.setAttribute(Constants.REQUEST_ORG_SELECT_BOX, accountService.getFuzzyOrgByOid(getSessionOrgId(),""));
    }
    
    /**
     * 根据个人所属机构 显示不同的机构下拉框， 市局组织显示所有<br/>
     * 会去掉 计安协会 项目
     * REQUEST_ORG_SELECT_BOX = "orgSelectBox"<br/>
     * 将List&lt;Org&gt; 设置在request中
     */
    protected void setOrgSelectBoxWithOutjaxhInRequest() {
    	List<Organization> orgList = accountService.getFuzzyOrgByOid(getSessionOrgId(),"");
    	for (int i = 0; i < orgList.size(); i++){
    		Organization org = orgList.get(i);
    		if ("计算机安全协会".equals(org.getFullName())){
    			orgList.remove(i);
    		}
    	}
        request.setAttribute(Constants.REQUEST_ORG_SELECT_BOX, orgList);
    }
    

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }
    
    public AccountService getAccountService() {
        return accountService;
    }
	public String getCtx() {
		HttpServletRequest httpServletRequest=(HttpServletRequest) request;
        ctx = httpServletRequest.getContextPath();
		return ctx;
	}
	public void setCtx(String ctx) {
		this.ctx = ctx;
	}

    
}
